//
//  LBSViewController.m
//  百度地图
//
//  Created by ljm on 2017/7/6.
//  Copyright © 2017年 ljm. All rights reserved.
//

#import "LBSViewController.h"

@interface LBSViewController ()<BMKCloudSearchDelegate>
{
    BMKMapView *_mapView;
    BMKCloudSearch *_cloudSearch;
}

@end

@implementation LBSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = @"BMKCloudmap";
    
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    [_mapView setTrafficEnabled:YES];
    
    [self.view addSubview:_mapView];

    
    /** 初始化云检索 **/
    _cloudSearch = [BMKCloudSearch new];
    _cloudSearch.delegate = self;
    
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(screenWidth - 80, screenHeight - 40, 80, 40)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"search" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchPlace) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void) searchPlace
{
    BMKCloudLocalSearchInfo *cloudLocalSearch = [[BMKCloudLocalSearchInfo alloc] init];
    /*
     * 本示例代码使用了测试ak和测试数据，开发者在检索自己LBS数据之前，需替换 cloudLocalSearch.ak
     * 和cloudLocalSearch.geoTableId的值
     *
     * 1、替换cloudLocalSearch.ak的值：
     * （1）请访问http://lbsyun.baidu.com/apiconsole/key申请一个“服务端”的ak，其他类型的ak无效；
     * （2）将申请的ak替换cloudLocalSearch.ak的值；
     *
     * 2、替换cloudLocalSearch.geoTableId值：
     * （1）申请完服务端ak后访问http://lbsyun.baidu.com/datamanager/datamanage创建一张表；
     * （2）在“表名称”处自由填写表的名称，如MyData，点击保存；
     * （3）“创建”按钮右方将会出现形如“MyData(34195)”字样，其中的“34195”即为geoTableId的值；
     * （4）添加或修改字段：点击“字段”标签修改和添加字段，然后保存；
     * （5）添加数据：
     *  a、标注模式：“数据” ->“标注模式”，输入要添加的地址然后“百度一下”，点击地图蓝色图标，再点击保存即可；
     *  b、批量模式： “数据” ->“批量模式”，可上传文件导入，具体文件格式要求请参见当页的“批量导入指南”；
     * （6）选择左边“设置”标签，“是否发布到检索”选择“是”，然后"保存";
     * （7）数据发布后，替换cloudLocalSearch.geoTableId的值即可；
     
     * 备注：每创建一张新表后需要选择发布到检索并保存，否则将会出现检索不到数据的情况
     */
    cloudLocalSearch.ak = BaiduMapKey;
    cloudLocalSearch.geoTableId = 31869;
    cloudLocalSearch.pageIndex = 0;
    cloudLocalSearch.pageSize = 10;
    
    cloudLocalSearch.region = @"北京市";
    cloudLocalSearch.keyword = @"天安门";
    BOOL flag = [_cloudSearch localSearchWithSearchInfo:cloudLocalSearch];

    if(flag){
        NSLog(@"本地云检索发送成功");
    }else{
        NSLog(@"本地云检索发送失败");
    }
}

- (void)onGetCloudPoiResult:(NSArray *)poiResultList searchType:(int)type errorCode:(int)error
{
    // 清楚屏幕中所有的annotation
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    if (error == BMKErrorOk) {
        BMKCloudPOIList* result = [poiResultList objectAtIndex:0];
        for (int i = 0; i < result.POIs.count; i++) {
            BMKCloudPOIInfo* poi = [result.POIs objectAtIndex:i];
            BMKPointAnnotation* item = [[BMKPointAnnotation alloc] init];
            CLLocationCoordinate2D pt = (CLLocationCoordinate2D){ poi.longitude,poi.latitude};
            item.coordinate = pt;
            item.title = poi.title;
            [_mapView addAnnotation:item];
        }
    } else {
        NSLog(@"error ==%d",error);
    }  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
